package com.kvest.sherlocktest;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.Toast;
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.ActionBar.Tab;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.kvest.sherlocktest.adapter.ViewPagerAdapter;
import com.kvest.sherlocktest.fragment.Fragment1;

public class TabbedActionBarActivity extends SherlockFragmentActivity implements ActionBar.TabListener, ViewPager.OnPageChangeListener {
    private ViewPager viewPager;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tabbed_layout);

        setContent();
        setActionBar();
    }

    private void setContent() {
        ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());

        //fill adapter
        adapter.addFragment(Fragment1.class, generateArguments("first", Color.RED, false, "frag1"));
        adapter.addFragment(Fragment1.class, generateArguments("Second", Color.GREEN, false, "frag2"));
        adapter.addFragment(Fragment1.class, generateArguments("Third", Color.BLUE, false, "frag3"));
        adapter.addFragment(Fragment1.class, generateArguments("fourth", Color.YELLOW, false, "frag4"));
        adapter.addFragment(Fragment1.class);

        viewPager = (ViewPager)findViewById(R.id.container_viewpager);
        viewPager.setOnPageChangeListener(this);
        viewPager.setAdapter(adapter);
    }

    private Bundle generateArguments(String text, int color, boolean hasOptionsMenu, String menuText) {
        Bundle arguments = new Bundle();
        arguments.putString(Fragment1.ARGUMENT_TEXT, text);
        arguments.putInt(Fragment1.ARGUMENT_COLOR, color);
        arguments.putBoolean(Fragment1.ARGUMENT_HAS_OPTIONS_MENU, hasOptionsMenu);
        arguments.putString(Fragment1.ARGUMENT_MENU_TEXT, menuText);

        return arguments;
    }

    public void setActionBar() {
        ActionBar actionBar = getSupportActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        actionBar.setCustomView(R.layout.custom_action_bar);
        actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);

        actionBar.getCustomView().findViewById(R.id.test_button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(getApplicationContext(), "test_button Click", Toast.LENGTH_SHORT).show();
            }
        });

        for (int i = 0 ; i < 5; ++i) {
            Tab tab = actionBar.newTab();
            tab.setText("tab" + (i + 1));
            tab.setTabListener(this);
            tab.setTag(new Integer(i));
            actionBar.addTab(tab);
        }
    }

    public void onClick(View view) {
        Toast.makeText(getApplicationContext(), "onClick", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        viewPager.setCurrentItem((Integer)tab.getTag());
    }

    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction ft) {}

    @Override
    public void onTabReselected(Tab tab, FragmentTransaction ft) {}

    @Override
    public void onPageScrolled(int i, float v, int i2) {}

    @Override
    public void onPageSelected(int i) {
        getSupportActionBar().selectTab(getSupportActionBar().getTabAt(i));
    }

    @Override
    public void onPageScrollStateChanged(int i) {}
}
